<?php
defined('OA_LIB_PATH') || define('OA_LIB_PATH', '/opt/op/OALib');

require_once 'PHPUnit/Framework.php';
require_once 'Log.php';
require_once OA_LIB_PATH . '/inner/open/models/OaAuthorityModel.php';

class OaAuthorityModelTest extends PHPUnit_Framework_TestCase
{
    private $_logger;
    private $_model;
    
    protected function setUp()
    {
        $logger = Log::factory('console');
        $logger->setMask(Log::MAX(PEAR_LOG_DEBUG));
        $this->_logger = $logger;
        $this->_model = new OaAuthorityModel($logger);
    }
    
    protected function tearDown()
    {
        
    }
    
    /**
     * @dataProvider provider
     */
    public function testAddOperation($role, $operation)
    {
        $this->markTestSkipped();
        $result = $this->_model->addOperation($operation);
        $this->assertTrue($result);
    }
    
    /**
     * @dataProvider provider
     */
    public function testDeleteOperation($role, $operation)
    {
        $this->markTestSkipped();
        $result = $this->_model->deleteOperation($operation);
        $this->assertTrue($result);
    }
    
    /**
     * @dataProvider provider
     */
    public function testAuthorize($role, $operation)
    {
        $this->markTestSkipped();
        $result = $this->_model->authorize($role, $operation);
        $this->assertTrue($result);
    }
    
    /**
     * @dataProvider provider
     */
    public function testRevoke($role, $operation)
    {
        $this->markTestSkipped();
        $result = $this->_model->revoke($role, $operation);
        $this->assertTrue($result);
    }
    
    public function testInitOperationTable()
    {
        $this->markTestSkipped();
        $ops = array(
            array('company.view', '访问公司'),
            array('company.create_app', '创建应用'),
            /*array('company.view_operation_data', '查看运营数据'),
            array('company.use_operation_tool', '使用运营工具'),
            array('company.check_account', '对账'),
            array('company.query_billing_order', '计费订单查询'),*/
            array('company.role_assign', '公司角色分配'),
            array('company.role_revoke', '公司角色回收'),
            array('company.role_transer', '公司角色转让'),
            //array('company.manage_test_account', '测试帐号管理'),
            array('company.point_assign', '分配点券'),
            array('company.app_admin_grant', '应用管理员指定'),
            array('app.view', '访问应用'),
            array('app.fill_in_basic_info', '填写基本信息'),
            array('app.select_service', '选择服务'),
            array('app.apply_for_testing', '申请测试'),
            array('app.production_config', '正式环境配置'),
            array('app.apply_for_release', '申请上线'),
            array('app.role_assign', '应用角色分配'),
            array('app.role_revoke', '应用角色收回'),
            array('app.view_operation_data', '查看运营数据'),
            array('app.use_operation_tool', '使用运营工具'),
            array('app.check_account', '对账'),
            array('app.query_billing_order', '计费订单查询')
            
        );
        foreach ($ops as $op) {
            $result = $this->_model->addOperation($op[0], $op[1]);
            $this->assertTrue($result);
        }
    }
    public function testCheckAppAuthority()
    {
        
        //$this->markTestSkipped();
        //测试app_admin
        /*$app_list = array('app.view'=> true,
            'app.fill_in_basic_info'=>true,
            'app.select_service'=>true,
            'app.apply_for_testing'=>true,
            'app.production_config'=>true,
            'app.apply_for_release'=>true,
            'app.role_assign'=>true,
            'app.role_revoke'=>true,
            'app.view_operation_data'=>true,
            'app.use_operation_tool'=>true,
            'app.check_account'=>true,
            'app.query_billing_order'=>true,
        	'app.manage_test_account'=>true
            );
        foreach($app_list as $op=>$re){
            $result = $this->_model->checkAppAuthority(691497365, 100000033, $op);
            $this->assertTrue($result===$re);  
        }*/
        //测试app.developer
        $app_list = array('app.view'=> true,
            'app.fill_in_basic_info'=>true,
            'app.select_service'=>true,
            'app.apply_for_testing'=>true,
            'app.production_config'=>true,
            'app.apply_for_release'=>false,
            'app.role_assign'=>false,
            'app.role_revoke'=>false,
            'app.view_operation_data'=>false,
            'app.use_operation_tool'=>false,
            'app.check_account'=>false,
            'app.query_billing_order'=>false,        	
        	'app.manage_test_account'=>false
            );
        foreach($app_list as $op=>$re){
            $result = $this->_model->checkAppAuthority(1213000637, 100000194, $op);
            $this->assertTrue($result===$re);  
        }
        //测试app.operator
        /*$app_list = array('app.view'=> true,
            'app.fill_in_basic_info'=>true,
            'app.select_service'=>true,
            'app.apply_for_testing'=>true,
            'app.production_config'=>true,
            'app.apply_for_release'=>false,
            'app.role_assign'=>false,
            'app.role_revoke'=>false,
            'app.view_operation_data'=>true,
            'app.use_operation_tool'=>true,
            'app.check_account'=>true,
            'app.query_billing_order'=>true,
            );
        foreach($app_list as $op=>$re){
            $result = $this->_model->checkAppAuthority(1180307581, 50834, $op);
            $this->assertTrue($result===$re);  
        }*/
    }
    public function testCheckCompanyAuthority()
    {
        $this->markTestSkipped();
        //测试sp_admin
        /*$app_list = array('app.view'=> true,
            'app.fill_in_basic_info'=>true,
            'app.select_service'=>true,
            'app.apply_for_testing'=>true,
            'app.production_config'=>true,
            'app.apply_for_release'=>true,
            'app.role_assign'=>true,
            'app.role_revoke'=>true,
            'app.view_operation_data'=>true,
            'app.use_operation_tool'=>true,
            'app.check_account'=>true,
            'app.query_billing_order'=>true
            );
        foreach($app_list as $op=>$re){
            $result = $this->_model->checkAppAuthority(1108960117, 50834, $op);
            $this->assertTrue($result===$re);  
        }
        $company_list = array(
            'company.view'=>true,
            'company.create_app'=>true,
            'company.point_assign'=>true,
            'company.role_assign'=>true
        );
        foreach($company_list as $op=>$re){
            $result = $this->_model->checkCompanyAuthority(1108960117, 37, $op);
            $this->assertTrue($result===$re);  
        }*/
        //测试sp_admin
        /*$app_list = array('app.view'=> true,
            'app.fill_in_basic_info'=>true,
            'app.select_service'=>true,
            'app.apply_for_testing'=>true,
            'app.production_config'=>true,
            'app.apply_for_release'=>true,
            'app.role_assign'=>true,
            'app.role_revoke'=>true,
            'app.view_operation_data'=>true,
            'app.use_operation_tool'=>true,
            'app.check_account'=>true,
            'app.query_billing_order'=>true
            );
        foreach($app_list as $op=>$re){
            $result = $this->_model->checkAppAuthority(1108960117, 50834, $op);
            $this->assertTrue($result===$re);  
        }*/
        $company_list = array(
            'company.view'=>true,
            'company.create_app'=>true,
            'company.point_assign'=>true,
        	'company.role_assign'=>true
        );
        foreach($company_list as $op=>$re){
            $result = $this->_model->checkCompanyAuthority(1213792587, 37, $op);
            $this->assertTrue($result===$re);  
        }
    }
    public function testInitAuthorityTable()
    {
        $this->markTestSkipped();
        $authorities = array(
            array('company.admin', 'company.*'),
            array('company.pm', 'company.view'),
            array('company.pm', 'company.create_app'),
            array('company.pm', 'company.point_assign'),
            array('company.employee', 'company.view'),
            array('company.employee', 'company.point_assign'),
            array('app.admin', 'app.*'),
            array('app.developer', 'app.view'),
            array('app.developer', 'app.fill_in_basic_info'),
            array('app.developer', 'app.select_service'),
            array('app.developer', 'app.apply_for_testing'),
            array('app.developer', 'app.production_config'),
            array('app.developer', 'app.use_operation_tool'),
            array('app.developer', 'app.view_operation_data'),
            array('app.developer', 'app.check_account'),
            array('app.developer', 'app.query_billing_order'),
            array('app.operator', 'app.view'),
            array('app.operator', 'app.fill_in_basic_info'),
            array('app.operator', 'app.select_service'),
            array('app.operator', 'app.apply_for_testing'),
            array('app.operator', 'app.production_config')
        );
        foreach ($authorities as $op) {
            $result = $this->_model->authorize($op[0], $op[1]);
            $this->assertTrue($result);
        }
    }
    public function testInitRoleTable()
    {
        $this->markTestSkipped();
        $roleList = OaAuthorityModel::$role;
        foreach($roleList as $k=>$row){
            $result = $this->_model->addRole($k, $row['name']);
            //$this->assertTrue($result);
        }
    }
    public function provider()
    {
        return array(
            array(OaAuthorityModel::ROLE_COMPANY_PM, 'company.create_app')
        );
    }
}
